﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
public partial class admin_现金助学_审核求助信息 : BLL.BasePage_Admin
{
    Appeal bll_Appeal = new Appeal();
    User_Volunteer bll_User_Volunteer = new User_Volunteer();
    Log_Check bll_Log_Check = new Log_Check();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UIInit();
        }
    }

    private void UIInit()
    {
        Detail_Bind();
        Bind_LogCheck();
    }

    private void Bind_LogCheck()
    {
        CheckList1.ObjectId = QueryString_Int("id");
        CheckList1.ObjectType = PublicEnum.ObjectType_Check.现金助学求助信息录入后的审核;
        CheckList1.NeedRefresh();
    }

    private void Detail_Bind()
    {
        int id = QueryString_Int("id");
        Model.Appeal m = bll_Appeal.GetModel(id);
        lblAddress.Text = m.Address;
        lblBirthday.Text = m.Birthday.ToString() + " 【" + Common.Helper_DateTime.Get_Age(m.Birthday).ToString() + "岁】";
        lblDemand.Text = m.Demand;
        lblFamilyIncome.Text = m.FamilyIncome;
        lblFamilyIncomeFrom.Text = m.FamilyIncomeFrom;
        lblFamilyInfo.Text = m.FamilyInfo;
        lblGrade_Class.Text = m.Grade_Class;
        string[] guardian1 = m.Guardian1.Split('|');
        lblGuardian1_Link.Text = guardian1[2];
        lblGuardian1_Name.Text = guardian1[0];
        lblGuardian1_Relation.Text = guardian1[1];
        string[] guardian2 = m.Guardian2.Split('|');
        lblGuardian2_Link.Text = guardian2[2];
        lblGuardian2_Name.Text = guardian2[0];
        lblGuardian2_Relation.Text = guardian2[1];
        lblName.Text = m.Name;
        lblPostalAddress.Text = m.PostalAddress;
        lblSchool.Text = m.School;
        string[] teacher = m.Teacher.Split('|');
        lblTeacher_Link.Text = teacher[1];
        lblTeacher_Name.Text = teacher[0];
        lblNationality.Text = m.Nationality;
        lblSex.Text = m.Sex;
        lblDistrict.Text = new Helper_District().Get_FullPath_DistrictId(m.DistrictId, Helper_District.ShowType.斜线连接);
        lblFrom.Text = m.From;
        chbIsPass.Checked = m.IsPass;
        if (m.IsPass1 == false && m.IsReject1 == false)
        {
            liCheck.Style["display"] = "block";
            lblTitle.Text = " ——【待审核】";
        }
        else
        {
            liCheck.Style["display"] = "none";
            lblTitle.Text = " ——已于 " + m.FinishTime.ToString() + " 由 " + bll_User_Volunteer.GetModel((int)m.VolunteerId).Nickname + " 审核,审核结果为：";
        }
        if (m.IsPass1)
        {
            Panel1.Visible = true;
            int temp = Convert.ToInt32(Common.StringDispose.SubString_Left(m.VolunteerIdStr,","));
            ucVolunteerAutoComplete.Text = bll_User_Volunteer.GetModel(temp).Nickname;
            ucVolunteerSelecter.VolunteerIdList = Common.StringDispose.Right(m.VolunteerIdStr, -(temp.ToString().Length + 1));
            lblTitle.Text += "通过。";
            lblTitle.ForeColor = System.Drawing.Color.Green;
        }
        else if (m.IsReject1)
        {
            lblTitle.Text += "拒绝。";
            lblTitle.ForeColor = System.Drawing.Color.Red;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int 主要负责人ID = 0;
        try
        {
            MyValidate();
            if (chbIsPass.Checked)
            {
                主要负责人ID = Get_UserId(ucVolunteerAutoComplete.Text);
                if (Common.StringDispose.Contains(ucVolunteerSelecter.VolunteerIdList, 主要负责人ID.ToString(), ","))
                {
                    throw new Exception("主要负责人【" + ucVolunteerAutoComplete.Text + "】不能出现在【其他负责人】列表中！");
                }
            }
        }
        catch (Exception ex)
        {
            Alert(ex.Message);
        }
        int id = QueryString_Int("id");

        Model.Log_Check log = bll_Log_Check.Get_LastLog(QueryString_Int("id"), PublicEnum.ObjectType_Check.现金助学求助信息录入后的审核);
        log.DisposeTime = DateTime.Now;
        log.IsDispose = true;
        log.Remark = txtReason.Text.Trim();
        log.Result = chbIsPass.Checked;
        log.User_CurrId = CurrentUser.Id;
        log.User_CurrName = CurrentUser.Nickname;

        if (!chbGoOnCheck.Checked)//结案
        {
            Model.Appeal m = bll_Appeal.GetModel(id);
            m.IsPass1 = chbIsPass.Checked;
            m.IsReject1 = !chbIsPass.Checked;
            if (m.IsPass1)
            {
                m.VolunteerId = CurrentUser.Id;
                m.VolunteerIdStr = 主要负责人ID.ToString() + "," + ucVolunteerSelecter.VolunteerIdList;
            }
            bll_Appeal.Update(m);
            //更新审查记录
            log.IsEnd = true;
            bll_Log_Check.Update(log);
            liCheck.Style["display"] = "none";
        }
        else//不结案
        {
            //新增审核任务
            Model.Log_Check task = new Model.Log_Check();
            task.BeforId = log.Id;
            task.Result = false;
            task.User_FromId = CurrentUser.Id;
            task.User_FromName = CurrentUser.Nickname;
            task.ObjectId = id;
            task.ObjectType = Convert.ToInt32(PublicEnum.ObjectType_Check.现金助学求助信息录入后的审核);
            log.AfterId = bll_Log_Check.Add(task);

            //更新当前审核任务
            log.IsEnd = false;
            log.User_NextName = txtNextChecker.Text.Trim();
            log.User_NextId = Get_UserId(log.User_NextName);
            log.Remark = txtReason.Text.Trim();
            bll_Log_Check.Update(log);
        }
        TabControlIndex.Value = "2";
        Alert("审核操作执行成功！");
        Bind_LogCheck();
    }

    private int Get_UserId(string name)
    {
        List<Model.User_Volunteer> list = bll_User_Volunteer.GetModelList("NickName = '" + name + "'");
        if (list.Count < 1)
        {
            throw new Exception("您输入的主要负责人不存在，请检查！");
        }
        return list[0].Id;
    }

    private bool MyValidate()
    {
        try
        {
            Check_NotEmpty(txtReason, "请填写批注，注明这样操作的理由！");
            if (chbIsPass.Checked)
            {
                Check_NotEmpty(ucVolunteerAutoComplete.Text, "请输入【主要负责人】！");
            }
        }
        catch (Exception ex)
        {
            Alert(ex.Message);
            return false;
        }
        return true;
    }
    protected void chbIsPass_CheckedChanged(object sender, EventArgs e)
    {
        if (chbIsPass.Checked)
        {
            Panel1.Visible = true;
            chbGoOnCheck.Visible = false;
        }
        else
        {
            chbGoOnCheck.Visible = true;
            Panel1.Visible = false;
            ucVolunteerAutoComplete.Text = string.Empty;
            ucVolunteerSelecter.VolunteerIdList = string.Empty;
        }
    }

    protected void chbGoOnCheck_CheckedChanged(object sender, EventArgs e)
    {
        if (chbGoOnCheck.Checked)
        {
            Panel2.Visible = true;
        }
        else
        {
            Panel2.Visible = false;
            txtNextChecker.Text = string.Empty;
        }
    }
}